#!/usr/bin/perl -w
use strict;
$| = 1;

#       Redistribution and use in source and binary forms, with or without
#       modification, are permitted provided that the following conditions are
#       met:
#       
#       * Redistributions of source code must retain the above copyright
#         notice, this list of conditions and the following disclaimer.
#       * Redistributions in binary form must reproduce the above
#         copyright notice, this list of conditions and the following disclaimer
#         in the documentation and/or other materials provided with the
#         distribution.
#       * Neither the name of the  nor the names of its
#         contributors may be used to endorse or promote products derived from
#         this software without specific prior written permission.
#       
#       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#	Copyright (C) 2008,2009 Aleksandr Deviatkin aka alid

#
# Коррекция часов счетчиков
# 2008-04-20 alid
#

use DBI;
use Carp;
use Time::Local;
use Fcntl qw (:DEFAULT :flock);

my (@args, %opts);
foreach(@ARGV){
   if(/^\-(\S)(.*)/){	$opts{$1} = $2;	} else {	push @args, $_;	}
}
my $verb = exists $opts{v};
my $force = exists $opts{f};	# игнорировать отсутствие флага mgroup.active
my ($database,$mgid,$flag) = (@args);
die "Usage: $0 database mgroup_id"	unless($database);
$mgid ||= 1;
$flag ||= 0;	# не переходить на летнее время (только для М203)
my $mode = '';	# a - подстройка часов, F - установка часов, иначе - просто показать время
if($opts{M} && $opts{M} eq 'a') {
	$mode = '-a';
} elsif($opts{M} && $opts{M} eq 'F') {
	$mode = '-F';
}
my $action230 = "$ENV{MY}counter/adjtime $mode";
my $action203 = "$ENV{MY}counter/adjtime203 $mode";

my $dbh = DBI->connect("dbi:Pg:dbname=$database","","",{AutoCommit => 0})	or die;

# список счетчиков
my $sth = $dbh->prepare("select counters.id,counters.name,counters.addr,counters.passwd,counters.passwd2,iface.dev,counters.ktrans,counter_type.type,status.id as status_id,status.state,status.pstate from counters inner join mgroup on mgroup.id=counters.mgroup inner join iface on mgroup.if_id=iface.id inner join counter_type on model=counter_type.id left outer join status on counters.id=status.cid where counters.active=1 and counters.mgroup=? ".($force? "":"and mgroup.active=1")." order by counters.id");

$sth->execute($mgid)	or die;
my $List;
while(my $row = $sth->fetchrow_hashref) {
	push @$List, $row;	
}
$sth->finish;
$dbh->disconnect;

# вот тут
foreach my $counter (@$List) {
	print "set: ".$counter->{addr}." ".$counter->{type}."..."	if $verb;
	if($counter->{type} =~ /M203/) {
		system("$action203 ".$counter->{addr}." ".$counter->{dev}." ".($verb ? '-v':'')." $flag");
	}
	elsif($counter->{type} =~ /M230/) {
		system("$action230 ".$counter->{addr}." ".(($mode =~ /F/)? $counter->{passwd2} : $counter->{passwd})." ".$counter->{dev}." ".($verb ? '-v':'')."");
	} else {
		print " ERROR: unknown device type [".$counter->{type}."]\n"	if $verb;
		next;
	}
	print "done\n"	if $verb;
}

